package com.ukefu.webim.util.impl;

import org.elasticsearch.common.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ukefu.core.UKDataContext;
import com.ukefu.util.ai.AiUtils;
import com.ukefu.util.event.AiEvent;
import com.ukefu.webim.service.repository.ChatMessageRepository;
import com.ukefu.webim.util.MessageUtils;
import com.ukefu.webim.util.disruptor.ai.AiMessageProcesserInterface;
import com.ukefu.webim.util.event.AiMessageAskProcesser;
import com.ukefu.webim.util.event.AiMessageSmartProcesser;
import com.ukefu.webim.util.router.OutMessageRouter;
import com.ukefu.webim.util.server.message.ChatMessage;
import com.ukefu.webim.web.model.AiConfig;
import com.ukefu.webim.web.model.MessageOutContent;

@Component
public class AiMessageProcesserImpl implements AiMessageProcesserInterface{
	
	
	@Autowired()
	private AiMessageAskProcesser aiMessageLevel1 ;
	
	@Autowired
	private AiMessageSmartProcesser aiMessageLevel2 ;
	
//	@Autowired
//	private AiMessageSmartProcesser aiMessageLevel3 ;
	
	@Autowired
	private ChatMessageRepository chatMessageRes ;

	/**
	 * 
	 */
	@Override
	public void onEvent(AiEvent event, long arg1, boolean arg2) {
		ChatMessage data = chat((ChatMessage) event.getEvent()) ;
		if(data != null ){
			MessageOutContent outMessage = MessageUtils.createAiMessage(data , data.getAppid(), data.getChannel(), UKDataContext.CallTypeEnum.OUT.toString() , UKDataContext.AiItemType.AIREPLY.toString() , UKDataContext.MediaTypeEnum.TEXT.toString(), data.getUserid()) ;
			
			if(!StringUtils.isBlank(data.getUserid()) && UKDataContext.MessageTypeEnum.MESSAGE.toString().equals(data.getType())){
	    		if(!StringUtils.isBlank(data.getTouser())){
		    		OutMessageRouter router = null ; 
		    		router  = (OutMessageRouter) UKDataContext.getContext().getBean(data.getChannel()) ;
		    		if(router!=null){
		    			router.handler(data.getTouser(), UKDataContext.MessageTypeEnum.MESSAGE.toString(), data.getAppid(), outMessage);
		    		}
		    	}
	    	}
		}
	}
	
	public ChatMessage chat(ChatMessage message){
		AiConfig aiConfig = AiUtils.initAiConfig(UKDataContext.SYSTEM_ORGI) ;
		ChatMessage data = null ;
		if(aiConfig.isAskfirst()){
			if(aiConfig.isEnableask()){
				data = aiMessageLevel1.process(message) ; 	//问答库查询
			}
			if(data == null && aiConfig.isEnablescene()){
				data = aiMessageLevel2.process(message) ;			//场景对话识别
			}
		}else{
			if(aiConfig.isEnablescene()){
				data = aiMessageLevel2.process(message) ;			//场景对话识别
			}
			if(data == null && aiConfig.isEnableask()){
				data = aiMessageLevel1.process(message) ; 	//问答库查询
			}
		}
		
			
		if(data == null && !StringUtils.isBlank(aiConfig.getNoresultmsg())){
			data = new ChatMessage();
			data.setMessage(aiConfig.getNoresultmsg());
			data.setAppid(message.getAppid());
			data.setUserid(message.getUserid());
			data.setUsername("小E");
			data.setChannel(message.getChannel());
			data.setType(message.getType());
			data.setContextid(message.getContextid());
			data.setOrgi(message.getOrgi());
		}
		return data ;
	}
	
	public ChatMessage chat(String message , String userid , String appid , String channel , String msgType , String contextid , String orgi , String session){
		ChatMessage chatMessage = new ChatMessage();
		chatMessage.setMessage(message);
		chatMessage.setAppid(!StringUtils.isBlank(appid) ? appid : UKDataContext.SYSTEM_ORGI);
		chatMessage.setUserid(userid);
		chatMessage.setUsername("小E");
		chatMessage.setChannel(channel);
		chatMessage.setCalltype(UKDataContext.CallTypeEnum.IN.toString());
		chatMessage.setType(UKDataContext.MediaTypeEnum.TEXT.toString());
		chatMessage.setContextid(contextid);
		chatMessage.setOrgi(orgi);
		
		chatMessageRes.save(chatMessage) ;
		ChatMessage retChateMessage = chat(chatMessage) ; 
		retChateMessage.setTouser(userid);
		retChateMessage.setUsession(session);
		retChateMessage.setMsgtype(msgType);
		retChateMessage.setCalltype(UKDataContext.CallTypeEnum.OUT.toString());
		chatMessageRes.save(retChateMessage) ;
		return  retChateMessage;
	}

}
